#pragma once
#include <iostream>
#include <string>
#include <unordered_set>

class InetShell
{
    bool IsWhite(const std::string &com)
    {
        auto pos = _whitelist.find(com);
        return pos != _whitelist.end();
    }

public:
    InetShell()
    {
        _whitelist.insert("ls");
        _whitelist.insert("ls -la");
        _whitelist.insert("ll");
        _whitelist.insert("pwd");
        _whitelist.insert("who");
        _whitelist.insert("whoami");
        _whitelist.insert("touch test.txt");
        _whitelist.insert("mkdir test");
    }
    std::string Execute(const std::string &com)
    {
        if (!IsWhite(com))
            return "unsafe";
        FILE *fp = popen(com.c_str(), "r");
        if (fp == NULL)
            return std::string();
        char buff[1024];
        std::string result;
        while (fgets(buff, sizeof(buff), fp))
        {
            result += buff;
        }
        pclose(fp);
        return result;
    }

private:
    std::unordered_set<std::string> _whitelist;
};